from werkzeug.security import generate_password_hash, check_password_hash

from .base import Base, db


class User(Base):

    __tablename__ = 'users'

    nick_name = db.Column(db.String(50), unique=True)
    real_name = db.Column(db.String(50))
    pwd = db.Column(db.String(128), nullable=False)
    sex = db.Column(db.String(1), nullable=False)
    email = db.Column(db.String(30), unique=True)
    phone = db.Column(db.String(13), unique=True)

    def __init__(self, role_code, org_code, nick_name, real_name, pwd, sex, email, phone, create_user, update_user):
        super(User, self).__init__(create_user, update_user)
        self.role_code = role_code
        self.org_code = org_code
        self.nick_name = nick_name
        self.real_name = real_name
        self.pwd = pwd
        self.sex = sex
        self.email = email
        self.phone = phone

    def hash_password(self):
        self.pwd = generate_password_hash(self.pwd)

    def verify_password(self, password):
        return check_password_hash(self.pwd, password)

    def __repr__(self):
        desc = 'User:{'
        for key in self.__dict__:
            desc += '%s:%s\n' % (key, self.__dict__[key])
        return desc % '}'
